package com.iot08.admin.controller;

import com.iot08.admin.controller.base.BaseController;
import com.iot08.admin.resp.VipMemberResp;
import com.iot08.admin.service.VipMemberService;
import com.iot08.admin.service.page.PageVO;
import com.iot08.admin.service.page.PageableImpl;
import com.iot08.admin.vo.VipMemberReqVo;
import com.iot08.common.entity.app.PxAdverster;
import lombok.extern.slf4j.Slf4j;
import org.bson.types.ObjectId;
import org.springframework.data.domain.Page;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;

/**
 * @author cwj
 * @description
 * @date 2025/4/17 8:45
 **/
@Slf4j
@RestController
@RequestMapping("vipMember")
public class VipMemberController extends BaseController {

    @Resource
    private VipMemberService vipMemberService;

    /**
     * @description 查询碎屏险的数据
     * @author cwj
     * @param [page]
     * @return org.springframework.web.servlet.ModelAndView
     * @date @time 2025/4/15 15:14
     **/
    @RequestMapping("list")
    public ModelAndView list(PageVO page, @RequestParam(value = "openid", required = false) String openid,
                             @RequestParam(value = "phone", required = false) String phone,
                             @RequestParam(value = "isVip", required = false) Integer isVip) {
        ModelAndView mv = new ModelAndView();
        Page<VipMemberResp> pages = vipMemberService.list(page,openid,phone,isVip);
        mv.addObject("vipMemberResps", pages.getContent());
        if (pages.getPageable() instanceof PageableImpl) {
            PageableImpl pi = (PageableImpl) pages.getPageable();
            mv.addObject("page", pi.getPage());
        }
        mv.addObject("openid",openid);
        mv.addObject("phone",phone);
        mv.addObject("isVip",isVip);
        mv.addObject("butSet", getMyBut(page.getMenuId()));
        mv.setViewName("vipMember/vip_member_list");
        return mv;
    }


    /**
     * @description
     * @author cwj
     * @param [id]
     * @return org.springframework.web.servlet.ModelAndView
     * @date @time 2025/4/17 18:08
     **/
    @RequestMapping("toEdit")
    public ModelAndView toEdit(String id) {
        ModelAndView mv = new ModelAndView();
        mv.addObject("vipMemberResp", vipMemberService.queryById(id));
        mv.setViewName("vipMember/vip_member_edit");
        return mv;
    }

    /**
     * @description 编辑会员
     * @author cwj
     * @param [vipMemberResp]
     * @return org.springframework.web.servlet.ModelAndView
     * @date @time 2025/4/18 10:29
     **/
    @RequestMapping(value = "edit", method = RequestMethod.POST)
    public ModelAndView edit(@Validated VipMemberReqVo vipMemberReqVo) {
        ModelAndView mv = new ModelAndView();
        if(ObjectUtils.isEmpty(vipMemberReqVo) || StringUtils.isEmpty(vipMemberReqVo.getId())){
            mv.addObject("msg", "fail");
            mv.setViewName("save_result");
            return mv;
        }
        vipMemberService.updateVipMember(vipMemberReqVo, this.getSessionUser(), this.getRemortIP());
        mv.addObject("msg", "success");
        mv.setViewName("save_result");
        return mv;
    }

}
